package testProject;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Query;
import com.google.apphosting.api.DatastorePb.DatastoreService;
import com.google.apphosting.utils.config.AppYaml.AdminConsole;
import com.onpositive.storage.managers.MemoryFileItemFactory;
import com.onpositive.storage.managers.ProfilesUsersManager;
import com.onpositive.storage.managers.UnifiedContentProvider;

public class UpdateSiteManagingServlet extends HttpServlet {
	private final static BlobstoreService bsservice = BlobstoreServiceFactory
			.getBlobstoreService();
	public final static String STORE_BLOB_SUFFIX = "/storeblob";
	public final static String GET_BLOB_SUFFIX = "/updatesite";

	public final static String LOGIN = "login";
	public final static String PASSWD = "passwd";

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String reqStr = req.getRequestURI();
		if (reqStr.startsWith(STORE_BLOB_SUFFIX)) {
			String login = req.getParameter(LOGIN);
			String passwd = req.getParameter(PASSWD);
			String correctURL = "";
			
			if(ProfilesUsersManager.isUserLoginned(login, passwd)){
				correctURL = bsservice.createUploadUrl(GET_BLOB_SUFFIX);
			}			
			resp.getWriter().write(correctURL);
		} else if (reqStr.startsWith(GET_BLOB_SUFFIX)) {
			Map<String, BlobKey> uploaded = bsservice.getUploadedBlobs(req);
			if (uploaded.size() == 1) {
				String[] k = uploaded.keySet().toArray(new String[1]);
				BlobKey bk = uploaded.get(k[0]);
				String correctFilePath = k[0];
				String bks = bk.getKeyString();
				UnifiedContentProvider.storeContentElement(correctFilePath, bks
						.getBytes());
			}
		}
		return;
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String reqStr = req.getRequestURI();
		if (reqStr.startsWith(STORE_BLOB_SUFFIX)) {
		} else if (reqStr.startsWith(GET_BLOB_SUFFIX)) {
			String mappedId = reqStr.substring(GET_BLOB_SUFFIX.length());
			String blobId = new String(UnifiedContentProvider
					.getContentElementAsBytes(mappedId), "UTF-8");
			BlobKey bc = new BlobKey(blobId);
			resp.setContentType("text");
			bsservice.serve(bc, resp);
			// bsservice.serve(bc, resp);
		}
	}
}
